/*
 * This class roughly wrapps a Map with freezable interface.
 *
 * It is not precise in that some operations (for instace removing all elements belonging to a collection that does not interset the curretn map keys) it will throw disallowed even though nothing was asked of it
 */
package org.yagnus.langutils.freezable;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.yagnus.interfaces.Freezable;
import org.yagnus.exceptions.DisallowedOperationException;

/**
 *
 * @author hc.busy
 */
public class FreezableMap<K, V> extends FreezableBase implements Map<K, V>, Freezable {

    Map<K, V> m;

    public FreezableMap(Map<K, V> m) {
        this.m = m;
    }

    public Collection<V> values() {
        return m.values();
    }

    public int size() {
        return m.size();
    }

    public V remove(Object key) {
        if (frozen && m.containsKey(key)) {
            throw new DisallowedOperationException();
        }
        return m.remove(key);
    }

    public void putAll(Map<? extends K, ? extends V> m) {
        if (frozen && m.size() > 0) {
            throw new DisallowedOperationException();
        }
        this.m.putAll(m);
    }

    public V put(K key, V value) {
        if (frozen) {
            throw new DisallowedOperationException();
        }
        return m.put(key, value);
    }

    public Set<K> keySet() {
        return m.keySet();
    }

    public boolean isEmpty() {
        return m.isEmpty();
    }

    public int hashCode() {
        return m.hashCode();
    }

    public V get(Object key) {
        return m.get(key);
    }

    public boolean equals(Object o) {
        return m.equals(o);
    }

    public Set<Entry<K, V>> entrySet() {
        return m.entrySet();
    }

    public boolean containsValue(Object value) {
        return m.containsValue(value);
    }

    public boolean containsKey(Object key) {
        return m.containsKey(key);
    }

    public void clear() {
        if (frozen && size() > 0) {
            throw new DisallowedOperationException();
        }
        m.clear();
    }
}
